package org.ctstudio.oa.duty.web;

import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.ctstudio.oa.duty.bean.DutyQuery;
import org.ctstudio.oa.duty.bean.DutyRecord;
import org.ctstudio.oa.duty.bean.DutySummary;
import org.ctstudio.oa.duty.dao.DutyBatchDAO;
import org.ctstudio.oa.duty.dao.DutyRecordsMapper;
import org.ctstudio.oa.duty.service.DutyCalcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DutyController extends AbstractController {

  @Autowired
  private DutyRecordsMapper mapper;

  @Autowired
  private DutyCalcService dutyCalcService;

  @RequestMapping(value = "/duty/list.do")
  public String dutyList(@ModelAttribute("query") DutyQuery query, Model model) {
    long total = mapper.count(query);
    model.addAttribute("total", total);
    if (total > 0) {
      List<DutyRecord> list = mapper.query(query);
      model.addAttribute("map", toMap(list));
      model.addAttribute("staffMap", this.getStaffIdNameMap(list));
    }
    return "duty/list";
  }

  /**
   * 按日期组织打卡记录
   * 
   * @param list
   * @return
   */
  private Map<LocalDate, List<DutyRecord>> toMap(List<DutyRecord> list) {
    Map<LocalDate, List<DutyRecord>> map = list.stream()
        .collect(Collectors.groupingBy(DutyRecord::getSignDate, LinkedHashMap::new, Collectors.toList()));
    return map;
  }

  @Autowired
  private DutyBatchDAO dao;

  @RequestMapping(value = "/duty/upgrade.do")
  @ResponseBody
  public String upgrade() {
    List<DutyRecord> list = mapper.getNeedUpgrade();
    dutyCalcService.calcWorkTime2(list);
    dao.batchUpgradeDutyRecord(list);
    return String.format("%d record upgrade succeed!", list.size());
  }

  @RequestMapping(value = "/duty/worktime/list.do")
  public String workTimeList(@ModelAttribute("query") DutyQuery query, Model model) {
    long total = mapper.countWorkTime(query);
    model.addAttribute("summaryType", 1);
    model.addAttribute("total", total);
    if (total > 0) {
      List<DutySummary> list = mapper.queryWorkTime(query);
      model.addAttribute("list", list);
      model.addAttribute("staffMap", this.getStaffIdNameMap(list));
    }
    return "duty/worktimelist";
  }

  @RequestMapping(value = "/duty/overtime/list.do")
  public String overTimeList(@ModelAttribute("query") DutyQuery query, Model model) {
    long total = mapper.countOverTime(query);
    model.addAttribute("summaryType", 2);
    model.addAttribute("total", total);
    if (total > 0) {
      List<DutySummary> list = mapper.queryOverTime(query);
      model.addAttribute("list", list);
      model.addAttribute("staffMap", this.getStaffIdNameMap(list));
    }
    return "duty/worktimelist";
  }
}
